//
//  WaveformGraphViewController.m
//  安好
//
//  Created by qinlongqing on 14-9-3.
//  Copyright (c) 2014年 冯凯. All rights reserved.
//

#import "WaveformGraphViewController.h"
#import "WaveformGraphView.h"

static NSInteger dataSourceCountIndex;//指向当前要操作的数据(数组中)
static NSInteger xCoordinateInMoniter;//在监护仪上的坐标(x坐标)

@interface WaveformGraphViewController ()
{
    NSTimer *timer;
    
    NSMutableArray *dataSource;
    
    PointContainer *pointContainer;
    
    WaveformGraphView *_translationMoniterView;
}
@end

@implementation WaveformGraphViewController

- (void)initData{
    dataSourceCountIndex = 0;
    xCoordinateInMoniter = 0;
    
    dataSource = [[NSMutableArray alloc]init];
    
    pointContainer = [[PointContainer alloc]init];
    [pointContainer initDataWithWidth:self.waveformGraphViewRect.size.width];
}

- (WaveformGraphView *)getTranslationMoniterView{
    if (!_translationMoniterView) {
//        if (IPHONE5_OR_LATER) {
//            _translationMoniterView = [[WaveformGraphView alloc]initWithFrame:CGRectMake(0, 340, 320, 154)];
//        }
//        else{
//            _translationMoniterView = [[WaveformGraphView alloc]initWithFrame:CGRectMake(0, 252, 320, 154)];
//        }
        _translationMoniterView = [[WaveformGraphView alloc]initWithFrame:CGRectMake(0, self.waveformGraphViewRect.origin.y + 10, self.waveformGraphViewRect.size.width, 154)];
        _translationMoniterView.backgroundColor = [UIColor clearColor];
        
        [self createWorkDataSourceWithTimeInterval:0.1];
    }
    return _translationMoniterView;
}

- (void)createWorkDataSourceWithTimeInterval:(NSTimeInterval)timeInterval{
    timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(timerTranslationFun) userInfo:nil repeats:YES];
}

- (void)timerTranslationFun{
    //分析摄像头扑捉的数据得到当前的心率
    [pointContainer addPointAsTranslationChangeForm:[self getTranslationPoint]];
    [_translationMoniterView fireDrawingWithPoints:[pointContainer translationPointContainer] pointsCount:[pointContainer numberOfTranslationElements]];
}

- (CGPoint)getTranslationPoint{
#pragma 从左到右
    CGPoint targetPointToAdd;
    @synchronized(dataSource){
//        static NSInteger dataSourceCountIndex = 0;//指向当前要操作的数据(数组中)
//        static NSInteger xCoordinateInMoniter = 0;//在监护仪上的坐标(x坐标)
        NSInteger pixelPerPoint = 4;//一次递增量
        if (dataSourceCountIndex < dataSource.count) {
            dataSourceCountIndex %=[dataSource count];//.....
            
            targetPointToAdd = (CGPoint){xCoordinateInMoniter,[self getYCoordinateInMoniter:[[dataSource objectAtIndex:dataSourceCountIndex] integerValue]]};
            xCoordinateInMoniter %= (int)(CGRectGetWidth(_translationMoniterView.frame));
            xCoordinateInMoniter += pixelPerPoint;
            
            dataSourceCountIndex++;
        }
        else if (dataSourceCountIndex == [dataSource count]) {
            targetPointToAdd = (CGPoint){xCoordinateInMoniter,87};
            xCoordinateInMoniter %= (int)(CGRectGetWidth(_translationMoniterView.frame));
            xCoordinateInMoniter += pixelPerPoint;
        }
    }
     return targetPointToAdd;
}

- (CGFloat)getYCoordinateInMoniter:(NSInteger)currentNumber{
    CGFloat height = _translationMoniterView.frame.size.height;
    if (currentNumber == 0) {
        return 87;
    }
    return height * (1 - (CGFloat)currentNumber/100);
}

- (void)addPointToDataSource:(NSInteger)current_Xin_Lv{
    @synchronized(dataSource){
        [dataSource addObject:[NSNumber numberWithInteger:current_Xin_Lv]];
//        NSLog(@"!!!!!!!!=%d",[dataSource.lastObject integerValue]);
    }
}

- (void)removeWaveformViewTimer{
    [timer invalidate];
}

- (void)dealloc{
//    NSLog(@"waveform graph view controller dealloc");
}

@end
